from __future__ import annotations

from dataclasses import dataclass
from typing import List, Dict


@dataclass
class Constraints:
    max_single_weight: float = 0.03
    max_positions: int = 3
    max_per_industry: int = 1

    def validate(self, positions: List[Dict]) -> List[str]:
        """检查硬性约束：持仓数量与行业集中度。"""
        errors: List[str] = []
        if len(positions) > self.max_positions:
            errors.append("Too many positions")
        # 行业约束检查：期望每个持仓包含 'industry' 字段
        industry_counts: Dict[str, int] = {}
        for pos in positions:
            industry = pos.get("industry", "UNKNOWN")
            industry_counts[industry] = industry_counts.get(industry, 0) + 1
        for ind, cnt in industry_counts.items():
            if cnt > self.max_per_industry:
                errors.append(f"Industry {ind} over limit: {cnt}")
        return errors




